package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.Class;
import com.fs.cv.pojo.pojodao.Student;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.StudentInfo;
import com.fs.cv.service.StudentInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @工程名: SSM项目
 * @描述：学生信息管理模块
 * @作者: 小梁子
 * @创建时间: 2020-09-29 15:36
 **/

@Controller
@RequestMapping("/studentInfo")
public class StudentInfoController {

    @Autowired
    private StudentInfoService studentService;

    //获取学生信息
    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String, Object> getStudentInfo(Integer currentPage,Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        /*System.out.println("打印的数据："+studentService.getAllInfo());*/
        PageInfo<StudentInfo> students = studentService.getStudentInfoByUserId(currentPage, pageSize);
        result.put("students",students.getList() );//返回查询的分页数据
        result.put("totalPage",students.getPages());//返回当前总页数
        result.put("code", 0);
        result.put("message", "获取数据成功");
        return result;
    }

    //增加一个学生信息
    @RequestMapping("/addStudentInfo")
    @ResponseBody
    public Map<String, Object> addUserInfo(StudentInfo studentInfo) {
        //获取当前登录的user
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");

        Class classs = studentService.getClassIdByUserId(user.getUserId());
        studentInfo.setClassId(classs.getClassId());

        Map<String, Object> result = new HashMap<>();
        result.put("code", studentService.addUserInfo(studentInfo));
        result.put("message", "获取数据成功");

        return result;
    }

    //通过学生ID获取一个学生信息
    @RequestMapping("/getOneStudentInfoById")
    @ResponseBody
    public Map<String, Object> getOneStudentInfoById(Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        result.put("student", studentService.getOneStudentInfoById(studentId));
        result.put("code", 0);
        return result;
    }

    //修改学生信息
    @RequestMapping("/editStudentInfo")
    @ResponseBody
    public Map<String, Object> editStudentInfo(Student student) {
        System.out.println("学生信息：" + student);
        Map<String, Object> result = new HashMap<>();
        result.put("code", studentService.editStudentInfo(student));
        return result;
    }

    //逻辑删除学生信息
    @RequestMapping("/deletedOneStudent")
    @ResponseBody
    public Map<String, Object> deletedOneStudent(Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        result.put("code",studentService.deletedOneStudent(studentId));
        return result;

    }

}
